# delimit ;
set more off ;
capture log close ;
clear all ;
version 16 ;

/* CODE FOR CHOICE EXPERIMENT SENSITIVITIES */
/* AUTHOR: ROGER H. VON HAEFEN  */
/* LAST EDITED: JANUARY 21, 2023 */

log using $sensitivities_log, replace text ;
log off ;

use $ce_data, replace ;

/* DROPPING:										*/
/*    1) SPEEDING PEOPLE (<=8 MINUTES TO COMPLETE SURVEY)				*/
/*    2) PEOPLE WHO TAKE MORE THAN A WEEK TO COMPLETE THE SURVEY			*/
/*    3) PEOPLE WHO FAIL THE TRAP QUESTION						*/
/*    4) PEOPLE WHO DISAGREE OR STRONGLY DISAGREE WITH THE CONSEQUENTIALITY STATEMENTS  */

keep if survey_duration > 8 & survey_duration <= (7*24*60) & inlist(debrief_3,"D") & !inlist(debrief_1,"D","SD") & !inlist(debrief_2,"D","SD") ;

rename gender male ;
local demos = "male age college white" ;

sort resp_id seq ;
preserve ;
   by resp_id : keep if _n == 1 ;
   log on ;
      sum `demos' ;
   log off ;
restore ;

/* REFORMATTING DATA */

sort resp_id seq ;
gen test = 2 ;
expand test ;
sort resp_id seq ;
by resp_id seq : replace test = 1 if _n == 1 ;
by resp_id seq : replace test = 0 if _n > 1  ;

replace ce = 1 - ce if test == 0 ;
replace cost = 0 if test == 0 ;
replace ec_g = 0 if test == 0 ;
replace ec_p = 0 if test == 0 ;
replace hr_g = 0 if test == 0 ;
replace hr_p = 0 if test == 0 ;
replace md_g = 0 if test == 0 ;
replace md_p = 0 if test == 0 ;
gen dummy = (test == 1) ;

replace test = sum(test) ;
rename test coid ;

gen mcost = - cost ;
gen mcost_dis = -cost/(1+near_dist) ;  /* FOR DISTANCE DECAY MODEL */

replace wgt_final = wgt_final/1000 ;

keep  ce coid resp_id dummy mcost* cost ec_g ec_p hr_g hr_p md_g md_p wgt_final block_f county order near_dist `demos' ;
order ce coid resp_id dummy mcost* cost ec_g ec_p hr_g hr_p md_g md_p wgt_final block_f county order near_dist `demos' ;

log on ;
foreach y of varlist `demos' { ;
   replace `y' = 0 if dummy == 0 ;
} ;
log off ;



/*log on ;
 eststo clear ;
 forv i = 3/6 { ;
   gen dis_thresh = (near_dist <= `i') ; sum dis_thresh if order == 1 & dummy == 1 ;
   foreach var of varlist ec_g ec_p hr_g hr_p md_g md_p { ;
      gen `var'_n = `var'*dis_thresh ;
   } ;   
   mixlogitwtp ce dummy ec_g_n ec_p_n hr_g_n hr_p_n md_g_n md_p_n ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) ;
   matrix define b0 = e(b) ;
   constraint 1 [SD]_b[mcost] = 0 ;
   matrix list b0 ;
   matrix b0[1,19] = 0 ;
   eststo: mixlogitwtp ce dummy ec_g_n ec_p_n hr_g_n hr_p_n md_g_n md_p_n ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) constraints(1) from(b0) ;
   test (ec_g_n == ec_p_n == hr_g_n == hr_p_n == md_g_n == md_p_n == 0) ;
   eststo: mixlogitwtp ce dummy ec_g_n ec_p_n hr_g_n hr_p_n md_g_n md_p_n `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p ) nrep(250) vce(robust) ;
   test (ec_g_n == ec_p_n == hr_g_n == hr_p_n == md_g_n == md_p_n == 0) ;
   drop ec_g_n ec_p_n hr_g_n hr_p_n md_g_n md_p_n dis_thresh ;
} ;
esttab using log\4-sensitivities_dist_decay.rtf, replace title(Distance Decay Alternative Specifications) mtitles("FIXED - 3 Miles" "RANDOM - 3 Miles" "FIXED - 4 Miles" "RANDOM - 4 Miles" "FIXED - 5 Miles" "RANDOM - 5 Miles" "FIXED - 6 Miles" "RANDOM - 6 Miles") scalars(ll) star(* .10 ** .05 *** .01) ;
*/


log on ;
eststo clear ;

/* PREFERENCE SPACE FIXED COEFFICIENT MODEL */

clogit ce cost ec_g ec_p hr_g hr_p md_g md_p dummy `demos' [pw=wgt_final], group(coid) cluster(resp_id) ;

/* #1 - WTP SPACE FIXED COEFFICIENT MODEL */

mixlogitwtp ce dummy ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) ;
matrix define b0 = e(b) ;
constraint 1 [SD]_b[mcost] = 0 ;
matrix list b0 ;
matrix b0[1,13] = 0 ;
eststo: mixlogitwtp ce dummy ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) constraints(1) from(b0) ;
local ll1 = e(ll) ; local par1 = e(k) ;

/* #2 - WTP SPACE RANDOM COEFFICIENT MODEL - BASELINE */

eststo: mixlogitwtp ce dummy `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) ;
local ll2 = e(ll) ; local par2 = e(k) ;
test ec_g == -ec_p ;
test hr_g == -hr_p ;
test md_g == -md_p ;
test (ec_g == -ec_p) (hr_g == -hr_p) (md_g == -md_p) ;
test (ec_g == -ec_p == hr_g == -hr_p) ;
test (ec_g == -ec_p == hr_g == -hr_p == md_g == -md_p) ;


/* #3 - WTP SPACE RANDOM COEFFICIENT MODEL - NO SAMPLING WEIGHTS IN ESTIMATION */

eststo: mixlogitwtp ce dummy `demos', group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) ;
local ll3 = e(ll) ; local par3 = e(k) ;


/* #4 - WTP SPACE RANDOM COEFFICIENT MODEL - ECOSYSTEM CONDITION FIRST */

eststo: mixlogitwtp ce dummy `demos' if block_f == 1 [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) ;
local ll4 = e(ll) ;  local par4 = e(k) ;

/* #5 - WTP SPACE RANDOM COEFFICIENT MODEL - MURKY WATER DAYS FIRST */

eststo: mixlogitwtp ce dummy `demos' if block_f == 0 [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) ;
local ll5 = e(ll) ;  local par5 = e(k) ;

/* HYPOTHESIS TEST - NULL = ORDERING OF ATTRIBUTES DOES NOT MATTER */

local ll_ur = `ll4' + `ll5' ; local par_ur = `par4' + `par5' ;
local ll_r  = `ll2' ;         local par_r  = `par2' ;
local test_stat = 2*(`ll_ur' - `ll_r') ;
local pval  = chi2tail(`par_ur'-`par_r',`test_stat') ;
local cval  = invchi2(`par_ur'-`par_r',.95) ;

di "" ;
di "HYPOTHESIS TEST - NULL = ORDERING OF ATTRIBUTES DOES NOT MATTER" ;
di "TEST STATISTIC:   " `test_stat' ;
di "CRITICAL VALUE:   " `cval' ;
di "P-VALUE (5%):     " `pval' ;
di "" ;     


/* #6 - WTP SPACE FIXED COEFFICIENT MODEL - FIRST CHOICE EXPERIMENT ONLY */

mixlogitwtp ce dummy ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final] if order == 1, group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) ;
matrix define b0 = e(b) ;
constraint 1 [SD]_b[mcost] = 0 ;
matrix list b0 ;
matrix b0[1,13] = 0 ;
eststo: mixlogitwtp ce dummy ec_g ec_p hr_g hr_p md_g md_p `demos' [pw=wgt_final] if order == 1, group(coid) id(resp_id) price(mcost) nrep(3) vce(robust) constraints(1) from(b0) ;
local ll6 = e(ll) ;  local par6 = e(k) ;

/* #7, #8, #9 - WTP SPACE RANDOM COEFFICIENT MODEL - COUNTY-SPECIFIC MODELS */

mixlogitwtp ce dummy `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(50) vce(robust) ;
matrix define b0 = e(b) ;
constraint 1 [SD]_b[mcost] = 0 ;
matrix list b0 ;
matrix b0[1,19] = 0 ;
mixlogitwtp ce dummy `demos' [pw=wgt_final]                 , group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) constraints(1) from(b0) ;
local llb = e(ll) ;  local parb = e(k) ;
eststo: mixlogitwtp ce dummy `demos' [pw=wgt_final] if county == "W", group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) constraints(1) from(b0) ;
local ll7 = e(ll) ;  local par7 = e(k) ;
eststo: mixlogitwtp ce dummy `demos' [pw=wgt_final] if county == "G", group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) constraints(1) from(b0) ;
local ll8 = e(ll) ;  local par8 = e(k) ;
eststo: mixlogitwtp ce dummy `demos' [pw=wgt_final] if county == "M", group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) constraints(1) from(b0) ;
local ll9 = e(ll) ;  local par9 = e(k) ;

/* HYPOTHESIS TEST - NULL = NO PREFERENCE HETEROGENEITY ACROSS COUNTIES */

local ll_ur = `ll7' + `ll8' + `ll9' ; local par_ur = `par7' + `par8' + `par9' - 3 ;
local ll_r  = `llb' ;         local par_r  = `parb' - 1 ;
local test_stat = 2*(`ll_ur' - `ll_r') ;
local pval  = chi2tail(`par_ur'-`par_r',`test_stat') ;
local cval  = invchi2(`par_ur'-`par_r',.95) ;

di "" ;
di "HYPOTHESIS TEST - NULL = NO PREFERENCE HETEROGENEITY ACROSS COUNTIES" ;
di "TEST STATISTIC:   " `test_stat' ;
di "CRITICAL VALUE:   " `cval' ;
di "P-VALUE (5%):     " `pval' ;
di "" ;     

/* #10 - NOW MODEL WITH ALL RESPONDENTS */

use $ce_data, replace ;

/* DROPPING:										*/
/*    1) SPEEDING PEOPLE (<=8 MINUTES TO COMPLETE SURVEY)				*/
/*    2) PEOPLE WHO TAKE MORE THAN A WEEK TO COMPLETE THE SURVEY			*/
/*    3) PEOPLE WHO FAIL THE TRAP QUESTION						*/
/*    4) PEOPLE WHO DISAGREE OR STRONGLY DISAGREE WITH THE CONSEQUENTIALITY STATEMENTS  */

*keep if survey_duration > 8 & survey_duration <= (7*24*60) & inlist(debrief_3,"D") & !inlist(debrief_1,"D","SD") & !inlist(debrief_2,"D","SD") ;

rename gender male ;
local demos = "male age college white" ;

sort resp_id seq ;
preserve ;
   by resp_id : keep if _n == 1 ;
   log on ;
      sum `demos' ;
   log off ;
restore ;

/* REFORMATTING DATA */

sort resp_id seq ;
gen test = 2 ;
expand test ;
sort resp_id seq ;
by resp_id seq : replace test = 1 if _n == 1 ;
by resp_id seq : replace test = 0 if _n > 1  ;

replace ce = 1 - ce if test == 0 ;
replace cost = 0 if test == 0 ;
replace ec_g = 0 if test == 0 ;
replace ec_p = 0 if test == 0 ;
replace hr_g = 0 if test == 0 ;
replace hr_p = 0 if test == 0 ;
replace md_g = 0 if test == 0 ;
replace md_p = 0 if test == 0 ;
gen dummy = (test == 1) ;

replace test = sum(test) ;
rename test coid ;

gen mcost = - cost ;
gen mcost_dis = -cost/(1+near_dist) ;  /* FOR DISTANCE DECAY MODEL */

replace wgt_final = wgt_final/1000 ;

keep  ce coid resp_id dummy mcost* cost ec_g ec_p hr_g hr_p md_g md_p wgt_final block_f county order `demos' ;
order ce coid resp_id dummy mcost* cost ec_g ec_p hr_g hr_p md_g md_p wgt_final block_f county order `demos' ;

log on ;
foreach y of varlist `demos' { ;
   replace `y' = 0 if dummy == 0 ;
} ;


log on ;

/* #10 - WTP SPACE RANDOM COEFFICIENT MODEL - ALL RESPONDENTS */

eststo: mixlogitwtp ce dummy `demos' [pw=wgt_final], group(coid) id(resp_id) price(mcost) rand(ec_g ec_p hr_g hr_p md_g md_p) nrep(250) vce(robust) ;
local ll10 = e(ll) ;  local par10 = e(k) ;

esttab using log\5-sensitivities.rtf, replace title(Alternative Specifications) mtitles("WTP-SPACE - FIXED" "WTP-SPACE - RANDOM" "NO WEIGHTS" "ECO COND FIRST" "MURKY WATER FIRST" "FIRST CE ONLY" "WAKE CO ONLY" "MECKLENBERG CO ONLY" "GUILFORD CO ONLY" "ALL RESPONDENTS") scalars(ll) star(* .10 ** .05 *** .01) ;

log off ;